#include<iostream>
#include<stack>
#include<vector>
using namespace std;
const int N = 1e4, M = 5e4;
vector<int> e[N];
stack<int> stk;
int dfn[N], low[N], instk[N], tot;
int scc_id[N], dout[N], sz[N], cnt, res;
void tarjan(int u){
    // 入栈，盖时间戳
    dfn[u] = low[u] = ++tot;
    stk.push(u);
    instk[u] = 1;
    for(auto v : e[u]){
        if(!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }else if(instk[v]){
            low[u] = min(low[u], low[v]);  //Q: 为什么不是low[v]
            // A: 
        }
    }
    if(dfn[u] == low[u]){
        cnt++;
        int v;
        do{
            v = stk.top();
            stk.pop();
            instk[v] = 0;
            scc_id[v] = cnt;
            sz[cnt]++;
        }while(u != v);
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        e[a].push_back(b);
    }
    for(int i = 1; i <= n; i++){
        if(!dfn[i]) tarjan(i);
    }
    // 统计每个点出度
    for(int i = 1; i <= n; i++){
        for(auto j : e[i]){
            if(scc_id[i] != scc_id[j]){
                dout[scc_id[i]]++;
            }
        }
    }
    int zerodout = 0;
    for(int i = 1; i <= cnt; i++){
        // printf("sz[%d]=%d\n", i, sz[i]);
        if(dout[i] == 0){
            res = sz[i];
            zerodout++;
        }
    }
    if(zerodout > 1){
        cout << 0;
    }else{
        cout << res;
    }
}